<?php

namespace backend\models\form;

use backend\models\AdminUser;
use common\models\Feed;
use common\service\ConstatService;
use yii\base\Model;
use Yii ;

class ChangeDealUserForm extends Model {

    public $deal_user_id;


    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['deal_user_id'],'required'],
            [['deal_user_id'], 'integer'],
            [['deal_user_id'], 'filter','filter'=>'\yii\helpers\HtmlPurifier::process']];
    }



    public function attributeLabels() {
        return [
            'deal_user_id' => '处理人' ,
        ] ;
    }

    public  function changeUser($id){
        if(empty($id)){
            return ["code"=>400,"msg"=>"ID不能空"];
        }

        $feed = Feed::findOne(["id"=>$id]) ;

        if(empty($feed)){
            return ["code"=>400,"msg"=>"不存在该反馈"];
        }

        $oldDealUserId = $feed['deal_user_id'] ;

        if($this->deal_user_id==$oldDealUserId){
            return ["code"=>400,"msg"=>"处理人一致不用修改"];
        }

        $user = AdminUser::findOne(["user_id"=>$this->deal_user_id]) ;

        if(empty($user)){
            return ["code"=>400,"msg"=>"用户不存在"];
        }

        $curData = date("Y-m-d H:i:s") ;
        $feed->deal_user_id = $this->deal_user_id ;
        $feed->deal_user_name = $user['username'] ;
        $feed->static = ConstatService::FEED_STATIC_DEALING ;
        $feed->upd_time = $curData ;

        $rs = $feed->update() ;

        if($rs==true){
            return ["code"=>200,"msg"=>"修改成功"];
        }

        $msg = current($feed->getFirstErrors()) ;
        return ["code"=>400,"msg"=>"修改失败-原因：".$msg];

    }



}
